package twoSum;

import java.util.HashMap;
import java.util.Map;

public class Solution2 {
    public int[] twoSum(int[] nums, int target){
        Map<Integer, Integer> hashMap = new HashMap<Integer, Integer>();
        int len = nums.length;
        for (int i = 0; i < len/2; i++){
            int n = target - nums[i];
            if (hashMap.containsKey(n)){
                return new int[]{hashMap.get(n), i};
            }
            hashMap.put(nums[i], i);
            n = target - nums[len - 1 - i];
            if (hashMap.containsKey(n)){
                return new int[]{hashMap.get(n), len - 1 - i};
            }
            hashMap.put(nums[len - 1 - i], len - 1 - i);
        }

        if (len%2 != 0){
            int midNum = nums[len/2];
            if (hashMap.containsKey(target - midNum)) {
                return new int[]{hashMap.get(target - midNum), len/2};
            }
        }
        throw new IllegalArgumentException("no two sum");
    }
}
